<template>
  <div class="loginBox">
    <div class="titleBox">
      <span>移动电表管理</span>
    </div>
    <div class="content">
      <div class="contentWrapper">
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          class="demo-ruleForm"
        >
          <el-form-item prop="account">
            <el-input
              v-model.trim="ruleForm.account"
              type="text"
              autocomplete="off"
              placeholder="账号"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="ruleForm.password"
              type="password"
              autocomplete="off"
              placeholder="请输入密码"
              maxlength="12"
              size="large"
            />
          </el-form-item>
          <!-- <div class="codeBox">
            <el-form-item prop="code">
              <el-input
                v-model.trim="ruleForm.code"
                type="text"
                autocomplete="off"
                placeholder="请输入验证码"
                maxlength="12"
                size="large"
              />
            </el-form-item>
            <div class="img" @click="getCodePictureData">
              <img style="width: 100%; height: 100%" :src="imgUrl" alt="" />
            </div>
          </div> -->
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm(ruleFormRef)"
              :loading="loginLoading"
              class="loginBtn"
              size="large"
              >立即登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue"
import { ElMessage } from "element-plus"
import type { FormInstance } from "element-plus"
import { useRouter } from "vue-router"
// import md5 from "md5"
import { login, getCodePicture, userInfo } from "@/api/index"
import { setSession } from "@/util/tools"

const loginLoading = ref(false)
const $router = useRouter()
const imgUrl = ref("")

// onMounted(() => {
//   getCodePictureData()
// })

const getCodePictureData = () => {
  getCodePicture().then((res) => {
    const blob = new Blob([res])
    const href = window.URL.createObjectURL(blob)
    imgUrl.value = href
  })
}

const ruleFormRef = ref<FormInstance>()

const validateAccount = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入账号!"))
  } else {
    callback()
  }
}
const validatePassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码!"))
  } else {
    callback()
  }
}

const validateCode = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入验证码!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  account: "",
  password: "",
  code: ""
})

const rules = reactive({
  account: [{ validator: validateAccount, trigger: "blur" }],
  password: [{ validator: validatePassword, trigger: "blur" }],
  code: [{ validator: validateCode, trigger: "blur" }]
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      // // const res = await userInfo()
      // setSession("token", "121212")
      // // setSession("userInfo", res.rows)
      // $router.push({ path: "/" })
      login({
        username: ruleForm.account,
        password: ruleForm.password,
        code: ruleForm.code
      }).then((res) => {
        if (res.code === 200) {
          setSession("token", "121212")
          setSession("userInfo", res.rows)
          $router.push({ path: "/" })
        } else {
          ElMessage.error(res.msg)
        }
      })
    } else {
      return false
    }
  })
}
</script>

<style scoped lang="less">
@import "./index.less";
</style>
